package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/viewOrder")
@SessionAttributes(value = {"account", "order", "cart","userInfo","userInfoList"})
public class OrderMController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/joinOrder")
    public String joinOrder(Model model){
        List<Order> listMOrder = orderService.getAllOrders("");
        model.addAttribute("orderList",listMOrder);
        return "managerOrder/orderManage";
    }


    @RequestMapping("/searchOrder")
    public String searchOrder(String userId, Model model) {
        List<Order> listMOrder = orderService.getAllOrders(userId);
        model.addAttribute("orderList",listMOrder);
        return "managerOrder/orderManage";
    }

    @RequestMapping("/modifyOrder")
    public String modifyOrder(int orderId,Model model){
        Order choosedOrder = orderService.selectOrderById(orderId);
        model.addAttribute("choosedOrder",choosedOrder);
        return "managerOrder/modifyOrder";
    }


    @RequestMapping("/confirmOrder")
    public String confirmOrder(int orderId, String username, String orderDate,String shipAddress1, String shipAddress2, String shipCity,
                              String shipState, String shipZip, String shipCountry, String billAddress1, String billAddress2,
                              String billCity, String billState, String billZip, String billCountry, String courier,
                              BigDecimal totalPrice, String billToFirstName, String billToLastName, String shipToFirstName,
                              String shipToLastName, String creditCard, String expiryDate, String cardType, String locale,
                              Model model) {
        Order order = new Order();
        order.setOrderId(orderId);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        try {
            date = simpleDateFormat.parse(orderDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        order.setOrderDate(date);
        order.setUsername(username);
        order.setShipAddress1(shipAddress1);
        order.setShipAddress2(shipAddress2);
        order.setShipCity(shipCity);
        order.setShipState(shipState);
        order.setShipZip(shipZip);
        order.setShipCountry(shipCountry);
        order.setBillAddress1(billAddress1);
        order.setBillAddress2(billAddress2);
        order.setBillCity(billCity);
        order.setBillState(billState);
        order.setBillZip(billZip);
        order.setBillCountry(billCountry);
        order.setCourier(courier);
        order.setTotalPrice(totalPrice);
        order.setBillToFirstName(billToFirstName);
        order.setBillToLastName(billToLastName);
        order.setShipToFirstName(shipToFirstName);
        order.setShipToLastName(shipToLastName);
        order.setCreditCard(creditCard);
        order.setExpiryDate(expiryDate);
        order.setCardType(cardType);
        order.setLocale(locale);

        System.out.println(locale);
        orderService.update(order);

        List<Order> listMOrder = orderService.getAllOrders("");
        model.addAttribute("orderList",listMOrder);

        return "/managerOrder/orderManage";
    }

    @RequestMapping("/deleteOrder")
    public String deleteOrder(int orderId,Model model){
        orderService.deleteOrder(orderId);
        List<Order> listMOrder = orderService.getAllOrders("");
        model.addAttribute("orderList",listMOrder);
        return "managerOrder/orderManage";
    }

    @RequestMapping("shipOrder")
    public String shipOrder(int orderId,boolean isShipped,Model model){
        orderService.updateStatus(orderId,isShipped);
        List<Order> listMOrder = orderService.getAllOrders("");
        model.addAttribute("orderList",listMOrder);
        return "/managerOrder/orderManage";
    }







}
